<?xml version="1.0" encoding="UTF-8"?>
<!--
	Copyright (C) 2010 Ethan Gruber
	EADitor: http://code.google.com/p/eaditor/
	Apache License 2.0: http://code.google.com/p/eaditor/
	
	Administrative EAD file list -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"
	xmlns:exist="http://exist.sourceforge.net/NS/exist" xmlns:xi="http://www.w3.org/2001/XInclude">

	<xsl:include href="../header.xsl"/>
	<xsl:include href="../footer.xsl"/>
	<xsl:include href="admin_menu.xsl"/>
	<xsl:param name="mode"/>
	<xsl:param name="page"/>	
	<xsl:param name="page_fixed" as="xs:integer">
		<xsl:choose>
			<xsl:when test="number($page)">
				<xsl:value-of select="$page"/>
			</xsl:when>
			<xsl:otherwise>1</xsl:otherwise>
		</xsl:choose>
	</xsl:param>
	<xsl:param name="display_path">../../</xsl:param>
	<xsl:param name="exist-url"/>
	<xsl:param name="solr-published-url"/>
	<xsl:param name="orbeon-url"/>

	<xsl:template match="/">
		<html>
			<head>
				<title>
					<xsl:value-of
						select="document(concat($exist-url, '/site_info.xml'))/site_info/title"/>
					<xsl:text>: Edit </xsl:text>
					<xsl:choose>
						<xsl:when test="$mode='ead'">
							<xsl:text>Coins</xsl:text>
						</xsl:when>
						<xsl:when test="$mode='vra'">
							<xsl:text>Artifacts</xsl:text>
						</xsl:when>
					</xsl:choose>
				</title>
				<link rel="stylesheet" type="text/css"
					href="http://yui.yahooapis.com/2.8.2r1/build/grids/grids-min.css"/>
				<link rel="stylesheet" type="text/css"
					href="http://yui.yahooapis.com/2.8.2r1/build/reset-fonts-grids/reset-fonts-grids.css"/>
				<link rel="stylesheet" type="text/css"
					href="http://yui.yahooapis.com/2.8.2r1/build/base/base-min.css"/>
				<link rel="stylesheet" type="text/css"
					href="http://yui.yahooapis.com/2.8.2r1/build/fonts/fonts-min.css"/>
				<!-- Core + Skin CSS -->
				<link rel="stylesheet" type="text/css"
					href="http://yui.yahooapis.com/2.8.2r1/build/menu/assets/skins/sam/menu.css"/>
				
				<link type="text/css" href="{$display_path}style.css" rel="stylesheet"/>
				<script type="text/javascript" language="javascript" src="{$display_path}javascript/jquery-1.3.2.min.js"/>
				<script type="text/javascript" language="javascript" src="{$display_path}javascript/show_delete_window.js"/>
			</head>
			<body class="yui-skin-sam">
				<div id="backgroundPopup"/>
				<div id="doc4" class="yui-t2">
					<xsl:call-template name="header"/>
					<div id="bd">
						<div id="yui-main">
							<div class="yui-b">
								<h2>
									<xsl:text>Edit </xsl:text>
									<xsl:choose>
										<xsl:when test="$mode='ead'">
											<xsl:text>Coins</xsl:text>
										</xsl:when>
										<xsl:when test="$mode='vra'">
											<xsl:text>Artifacts</xsl:text>
										</xsl:when>
									</xsl:choose>
								</h2>
								<p>
									<a href="{$display_path}admin">&lt; Return</a>
								</p>
								<p>
									<a href="../create/{$mode}">Create New <xsl:choose>
											<xsl:when test="$mode='ead'">
												<xsl:text>Coin</xsl:text>
											</xsl:when>
											<xsl:when test="$mode='vra'">
												<xsl:text>Artifact</xsl:text>
											</xsl:when>
										</xsl:choose></a>
									<br/>
									<xsl:if test="$mode = 'ead'">
										<a href="../collection/edit">Edit Collection Information</a>
									</xsl:if>
								</p>
								<xsl:if test="//exist:resource">
									<h3>List of <xsl:choose>
											<xsl:when test="$mode='ead'">
												<xsl:text>Coins</xsl:text>
											</xsl:when>
											<xsl:when test="$mode='vra'">
												<xsl:text>Artifacts</xsl:text>
											</xsl:when>
										</xsl:choose></h3>
									<xsl:call-template name="paging"/>
									<table style="width:100%">
										<tr>
											<th style="width:70%">Title</th>
											<th style="width:10%">View</th>
											<th style="width:10%">Publish</th>
											<th style="width:10%">Delete</th>
										</tr>

										<xsl:choose>
											<xsl:when test="number($page_fixed)">
												<xsl:apply-templates
												select="//exist:resource[position() &gt;= (($page_fixed - 1) * 10) + 1 and position() &lt;= $page_fixed * 10]"
												/>
											</xsl:when>
											<xsl:otherwise>
												<xsl:apply-templates
												select="//exist:resource[position() &lt;= 10]"
												/>
											</xsl:otherwise>
										</xsl:choose>

									</table>
									<div class="delete_window" style="display:none">
										<div class="trigger_container_header">
											<h3 style="margin-left:10px;"/>
											<div class="close">close</div>
										</div>
										<iframe src="" scrolling="no" frameborder="0"
											style="width:100%"/>
									</div>
								</xsl:if>
							</div>
						</div>
						<xsl:call-template name="admin_menu"/>
					</div>
					<xsl:call-template name="footer"/>
				</div>
			</body>
		</html>
	</xsl:template>

	<xsl:template match="exist:resource[string(substring-before(@name, '.xml'))]">
		<xsl:variable name="file" select="concat($exist-url, '/', $mode, '/', @name)"/>
		<xsl:variable name="class">
			<xsl:choose>
				<xsl:when test="position() mod 2 = 0">even-row</xsl:when>
				<xsl:otherwise>odd-row</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="id">
			<xsl:choose>
				<xsl:when test="$mode = 'ead'">
					<xsl:value-of select="document($file)/c/@id"/>
				</xsl:when>
				<xsl:when test="$mode = 'vra'">
					<xsl:value-of select="document($file)/vra/work/@id"/>
				</xsl:when>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="title">
			<xsl:choose>
				<xsl:when test="$mode = 'ead'">
					<xsl:value-of select="normalize-space(document($file)//unittitle)"/>
					<xsl:text> (</xsl:text>
					<xsl:value-of select="document($file)/c/@id"/>
					<xsl:text>)</xsl:text>
				</xsl:when>
				<xsl:when test="$mode = 'vra'">
					<xsl:value-of select="normalize-space(document($file)//titleSet/title)"/>
					<xsl:text> (</xsl:text>
					<xsl:value-of select="document($file)/vra/work/@id"/>
					<xsl:text>)</xsl:text>
				</xsl:when>
			</xsl:choose>

		</xsl:variable>
		<tr>
			<td class="{$class}">
				<h3>
					<a href="../edit/{$id}?mode={$mode}">
						<xsl:value-of select="$title"/>
					</a>
				</h3>
				<div>
					<xsl:value-of select="document($file)//unitdate"/>
				</div>
			</td>
			<td class="{$class}" style="text-align:center">
				<a href="{$display_path}display/{$mode}/{$id}.xml" target="_blank">xml</a> | <a
					href="{$display_path}display/{$mode}/{$id}" target="_blank">html</a>
			</td>
			<td class="{$class}" style="text-align:center">
				<!-- form solr query to test whether or not it has been published -->
				<xsl:variable name="query"
					select="concat($solr-published-url, 'select/?q=id:', $id)"/>

				<xsl:choose>
					<xsl:when test="document($query)//result[@name='response']/@numFound = '0'">
						<input type="checkbox" class="publish_checkbox" id="publish-{$id}"
							baseurl="{$orbeon-url}" datatype="{$mode}" title="{$title}"/>
					</xsl:when>
					<xsl:otherwise>
						<input type="checkbox" class="publish_checkbox" id="publish-{$id}"
							baseurl="{$orbeon-url}" datatype="{$mode}" title="{$title}"
							checked="checked"/>
					</xsl:otherwise>
				</xsl:choose>

			</td>
			<td class="{$class}" style="text-align:center">
				<a class="delete_button" id="delete-{$id}" title="{$title}" baseurl="{$orbeon-url}"
					datatype="{$mode}">
					<img src="{$display_path}images/remove.gif"/>
				</a>
			</td>
		</tr>
	</xsl:template>

	<xsl:template name="paging">
		<xsl:variable name="start_var">
			<xsl:value-of select="$page_fixed"/>
		</xsl:variable>

		<xsl:variable name="numFound">
			<xsl:value-of select="count(//exist:resource)"/>
		</xsl:variable>

		<xsl:variable name="next">
			<xsl:value-of select="$start_var+1"/>
		</xsl:variable>

		<xsl:variable name="previous">
			<xsl:choose>
				<xsl:when test="$start_var &gt;= 1">
					<xsl:value-of select="$start_var - 1"/>
				</xsl:when>
				<xsl:otherwise>1</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="current" select="$start_var"/>
		<xsl:variable name="total" select="ceiling($numFound div 10)"/>

		<div class="paging_div">
			<div style="float:left;">
				<xsl:text>Displaying records </xsl:text>
				<b>
					<xsl:value-of select="(($start_var - 1) * 10) + 1"/>
				</b>
				<xsl:text> to </xsl:text>
				<xsl:choose>
					<xsl:when test="$numFound &gt; ($start_var * 10)">
						<b>
							<xsl:value-of select="($start_var * 10)"/>
						</b>
					</xsl:when>
					<xsl:otherwise>
						<b>
							<xsl:value-of select="$numFound"/>
						</b>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:text> of </xsl:text>
				<b>
					<xsl:value-of select="$numFound"/>
				</b>
				<xsl:text> total results.</xsl:text>
			</div>

			<!-- paging functionality -->
			<div style="float:right;">
				<xsl:choose>
					<xsl:when test="$start_var &gt; 1">
						<a class="pagingBtn" href="?page={$previous}">«Previous</a>
					</xsl:when>
					<xsl:otherwise>
						<span class="pagingSep">«Previous</span>
					</xsl:otherwise>
				</xsl:choose>

				<!-- always display links to the first two pages -->
				<xsl:if test="$start_var &gt; 3">
					<a class="pagingBtn" href="?page=1">
						<xsl:text>1</xsl:text>
					</a>
				</xsl:if>
				<xsl:if test="$start_var &gt; 4">
					<a class="pagingBtn" href="?page=2">
						<xsl:text>2</xsl:text>
					</a>
				</xsl:if>

				<!-- display only if you are on page 6 or greater -->
				<xsl:if test="$start_var &gt; 5">
					<span class="pagingSep">...</span>
				</xsl:if>

				<!-- always display links to the previous two pages -->
				<xsl:if test="$start_var &gt; 2">
					<a class="pagingBtn" href="?page={$start_var - 2}">
						<xsl:value-of select="$start_var - 2"/>
					</a>
				</xsl:if>
				<xsl:if test="$start_var &gt; 1">
					<a class="pagingBtn" href="?page={$start_var - 1}">
						<xsl:value-of select="$start_var - 1"/>
					</a>
				</xsl:if>

				<span class="pagingBtn">
					<b>
						<xsl:value-of select="$current"/>
					</b>
				</span>

				<!-- next two pages -->
				<xsl:if test="$start_var + 1 &lt;= $total">
					<a class="pagingBtn" href="?page={$start_var + 1}">
						<xsl:value-of select="$start_var + 1"/>
					</a>

				</xsl:if>
				<xsl:if test="$start_var + 2 &lt;= $total">
					<a class="pagingBtn" href="?page={$start_var + 2}">
						<xsl:value-of select="$start_var + 2"/>
					</a>

				</xsl:if>
				<xsl:if test="$start_var &lt;= $total - 4">
					<span class="pagingSep">...</span>
				</xsl:if>

				<!-- last two pages -->
				<xsl:if test="$start_var &lt;= $total - 4">
					<a class="pagingBtn" href="?page={$total - 1}">
						<xsl:value-of select="$total - 1"/>
					</a>
				</xsl:if>
				<xsl:if test="$start_var &lt;= $total - 3">
					<a class="pagingBtn" href="?page={$total}">
						<xsl:value-of select="$total"/>
					</a>
				</xsl:if>

				<xsl:choose>
					<xsl:when test="$total &gt; $current">
						<a class="pagingBtn" href="?page={$next}">Next»</a>
					</xsl:when>
					<xsl:otherwise>
						<span class="pagingSep">Next»</span>
					</xsl:otherwise>
				</xsl:choose>
			</div>
		</div>
	</xsl:template>

</xsl:stylesheet>
